define(["jquery","layuiall","zTree","jqform"],function ($,layui,zTree,jqform) {
    var form = layui.form;
    controller = {
        index:function () {
            form.on('switch(status)', function(data){
                var status = 0;
                if(data.elem.checked){
                    status = 1
                }
                $.ajax({
                    type:'post',
                    data:{
                        id:data.value,
                        status:status
                    },
                    url:$(data.elem).attr("data-url"),
                    success:function (res) {
                        layer.msg(res.msg)
                    }
                })
            });
            jqform.delete()

            //数据全部选中
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },
        addapiuser:function () {
            layui.use(["selectInput"],function () {
                var  selectInput= layui.selectInput;
                var ins = selectInput.render({
                    elem: '#user',
                    name: '',
                    initValue:'',
                    data: [
                        {value: '', name: ''},
                    ],
                    remoteSearch:true,
                    placeholder: '请输入用户',
                    url: "",
                    remoteMethod: function (value, cb) {
                        if (!value) {
                            return cb([]);
                        }
                        setTimeout(function () {
                            $.ajax({
                                method: 'get',
                                url: '/admin/user.apiuser/addapiuser',
                                data: {
                                    dataType:'user',
                                    username: value,
                                },
                                success: function (data) {
                                    return cb(data)
                                }
                            })
                        },1000)
                    }
                })
                // 监听select 选择事件
                ins.on('itemSelect(user)', function (obj) {
                    $("input[name='uid']").val(obj.data)
                });

            })
            jqform.init()
        },
        editapiuser:function () {
            layui.use(["selectInput"],function () {
                var selectInput= layui.selectInput;
                var datas = JSON.parse($("#user").attr("data"));
                console.log(datas)
                var ins = selectInput.render({
                    elem: '#user',
                    name: '',
                    initValue:datas.username,
                    data: [
                        {value: datas.id, name:datas.username},
                    ],
                    remoteSearch:true,
                    placeholder: '请输入用户',
                    url: "",
                    remoteMethod: function (value, cb) {
                        if (!value) {
                            return cb([]);
                        }
                        setTimeout(function () {
                            $.ajax({
                                method: 'get',
                                url: '/admin/user.apiuser/addapiuser',
                                data: {
                                    dataType:'user',
                                    username: value,
                                },
                                success: function (data) {
                                    return cb(data)
                                }
                            })
                        },1000)
                    }
                })
                // 监听select 选择事件
                ins.on('itemSelect(user)', function (obj) {
                    $("input[name='uid']").val(obj.data)
                });

            })
            jqform.init()
        }
    }
    return controller;
})